package com.gljx.web.service.impl;

import com.gljx.util.BigDecimalFormat;
import com.gljx.util.GetFid;
import com.gljx.web.Entity.TrainingCharge;
import com.gljx.web.Entity.TrainingChargeEntry;
import com.gljx.web.Entity.User;
import com.gljx.web.dao.CommonDao;
import com.gljx.web.dao.TrainingChargeEntryMapper;
import com.gljx.web.dao.TrainingChargeMapper;
import com.gljx.web.service.TrainingChargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TrainingChargeServiceImpl extends CommonDao implements TrainingChargeService{

	@Autowired
	private TrainingChargeEntryMapper trainingChargeEntryMapper;
	
	@Autowired
	private TrainingChargeMapper trainingChargeMapper;
	
	/**
	 *取分页数据    dmz 20160921
	 *@param pindex 页码
	 *@param psql SQL语句，需要带 rownum rn 例如：select rownum rn,t.* from zzda t
	 *@param psize 每页记录数
	 * */
	@Override
	public Map<String, Object> selectPage(int pindex, String psql, int psize) {
		  Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Pindex", pindex);
	       paramMap.put("Psql", psql);
	       paramMap.put("Psize", psize);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       paramMap.put("v_cur", oracle.jdbc.OracleTypes.CURSOR);
	       this.getSqlSession().select("com.gljx.web.dao.TrainingChargeMapper.pagemap", paramMap, null);
	       return  paramMap;
	}

	/**
	 * 取记录总数      dmz 20160921
	 * @param psql   SQL语句
	 * @param pcount 记录总数
	 * @return
	 */
	@Override
	public Map<String, Object> selectPageCount(String psql) {
		   Map<String, Object> paramMap = new HashMap<String, Object>();
	       paramMap.put("Psql", psql);
	       paramMap.put("Pcount", oracle.jdbc.OracleTypes.INTEGER);
	       this.getSqlSession().select("com.gljx.web.dao.TrainingChargeMapper.pagecount", paramMap, null);
	       return paramMap;
	}

	/**
	 * 实现根据条件查询
	 */
	@Override
	public List<TrainingChargeEntry> quertTrainingChargeEntryByInfo(String parentId,String drivingName, String cfcar) {
		// TODO Auto-generated method stub
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("v_fparentid", parentId);
		paramMap.put("v_drivingName", drivingName);
		paramMap.put("v_cfcar", cfcar);
		paramMap.put("v_cursor", oracle.jdbc.OracleTypes.CURSOR);
		this.getSqlSession().select("com.gljx.web.dao.TrainingChargeEntryMapper.selectTrainingChargeEntry", paramMap, null);
		@SuppressWarnings("unchecked")
		List<TrainingChargeEntry> trainingChargeEntrys = (List<TrainingChargeEntry>) paramMap.get("v_cursor");
		return trainingChargeEntrys;
	}

	/**
	 * 实现添加或修改
	 */
	@Override
	public void editTrainingCharge(TrainingCharge trainingCharge, String[] fseq, String[] cfcar, String[] cfcartype,
			String[] cfjiaxiaoid, String[] cftime, String[] cffandian, String[] cfzsc, String[] cfprice,
			String[] cfamount, String[] cfnumber, String[] cfremark,User user,String time) {
		// TODO Auto-generated method stub
		if("".equals(trainingCharge.getFid()) || trainingCharge.getFid() == null){
			//创建人id
			trainingCharge.setFcreatorid(user.getFid());
			//利用UUID生成fid
			trainingCharge.setFid(GetFid.UUIDIntoFid());
			//当前时间
			trainingCharge.setFcreatetime(time);
			//状态
			trainingCharge.setCfbillstate(4L);
			//添加
			trainingChargeMapper.insertSelective(trainingCharge);
			if(fseq == null || fseq.length == 0){
				
			}else{
				for (int i = 0; i < fseq.length; i++) {
					TrainingChargeEntry trainingChargeEntry = new TrainingChargeEntry();
					trainingChargeEntry.setCfcar(cfcar[i]);
					trainingChargeEntry.setCfcartype(cfcartype[i]);
					trainingChargeEntry.setCfjiaxiaoid(cfjiaxiaoid[i]);
					if("".equals(cftime[i])){
						
					}else{
						trainingChargeEntry.setCftime(BigDecimalFormat.formot(cftime[i]));
					}
					if("".equals(cffandian[i])){
						
					}else{
						trainingChargeEntry.setCffandian(BigDecimalFormat.formot(cffandian[i]));
					}
					if("".equals(cfzsc[i])){
						
					}else{
						trainingChargeEntry.setCfzsc(BigDecimalFormat.formot(cfzsc[i]));
					}
					if("".equals(cfprice[i])){
						
					}else{
						trainingChargeEntry.setCfprice(BigDecimalFormat.formot(cfprice[i]));
					}
					if("".equals(cfamount[i])){
						
					}else{
						trainingChargeEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
					}
					if("".equals(cfnumber[i])){
					}else{
						trainingChargeEntry.setCfnumber(Long.parseLong(cfnumber[i]));
					}
					trainingChargeEntry.setCfremark(cfremark[i]);
					trainingChargeEntry.setFseq((long)i+1);
					trainingChargeEntry.setFid(GetFid.UUIDIntoFid());
					trainingChargeEntry.setFparentid(trainingCharge.getFid());
					trainingChargeEntryMapper.insertSelective(trainingChargeEntry);
				}
			}
		}else{
			//修改时间时间
			trainingCharge.setFlastupdatetime(time);
			//修改人id
			trainingCharge.setFlastupdateuserid(user.getFid());
			//修改
			trainingChargeMapper.updateByPrimaryKeySelective(trainingCharge);
			//先删除子类信息
			trainingChargeEntryMapper.deleteByFparentId(trainingCharge.getFid());
			if(fseq == null || fseq.length == 0){
				
			}else{
				for (int i = 0; i < fseq.length; i++) {
					TrainingChargeEntry trainingChargeEntry = new TrainingChargeEntry();
					trainingChargeEntry.setCfcar(cfcar[i]);
					trainingChargeEntry.setCfcartype(cfcartype[i]);
					trainingChargeEntry.setCfjiaxiaoid(cfjiaxiaoid[i]);
					if("".equals(cftime[i])){
						
					}else{
						trainingChargeEntry.setCftime(BigDecimalFormat.formot(cftime[i]));
					}
					if("".equals(cffandian[i])){
						
					}else{
						trainingChargeEntry.setCffandian(BigDecimalFormat.formot(cffandian[i]));
					}
					if("".equals(cfzsc[i])){
						
					}else{
						trainingChargeEntry.setCfzsc(BigDecimalFormat.formot(cfzsc[i]));
					}
					if("".equals(cfprice[i])){
						
					}else{
						trainingChargeEntry.setCfprice(BigDecimalFormat.formot(cfprice[i]));
					}
					if("".equals(cfamount[i])){
						
					}else{
						trainingChargeEntry.setCfamount(BigDecimalFormat.formot(cfamount[i]));
					}
					if("".equals(cfnumber[i])){
						
					}else{
						trainingChargeEntry.setCfnumber(Long.parseLong(cfnumber[i]));
					}
					trainingChargeEntry.setCfremark(cfremark[i]);
					trainingChargeEntry.setFseq((long)i+1);
					trainingChargeEntry.setFid(GetFid.UUIDIntoFid());
					trainingChargeEntry.setFparentid(trainingCharge.getFid());
					trainingChargeEntryMapper.insertSelective(trainingChargeEntry);
				}
			}
		}
	}

	/**
	 * 实现删除
	 */
	@Override
	public void deleteTrainingChargeByFid(String[] deleteId, String fid) {
		// TODO Auto-generated method stub
		if(fid == null || "".equals(fid)){
			for (int i = 0; i < deleteId.length; i++) {
				//先删除子类信息
				trainingChargeEntryMapper.deleteByFparentId(deleteId[i]);
				//下删除父类信息
				trainingChargeMapper.deleteByPrimaryKey(deleteId[i]);
			}
		}else{
			//先删除子类信息
			trainingChargeEntryMapper.deleteByFparentId(fid);
			//下删除父类信息
			trainingChargeMapper.deleteByPrimaryKey(fid);
		}
		
	}

	/**
	 * 根据fid查询
	 */
	@Override
	public TrainingCharge queryTrainingByFid(String fid) {
		// TODO Auto-generated method stub
		return trainingChargeMapper.selectTrainingByFid(fid);
	}

	/**
	 * 审核，反审核
	 */
	@Override
	public int auditTrainingCharge(String fid, String type,String fhandlerId) {
		// TODO Auto-generated method stub
		TrainingCharge trainingCharge = new TrainingCharge();
		//fid
		trainingCharge.setFid(fid);
		//操作人id
		trainingCharge.setFauditorid(fhandlerId);
		if("1".equals(type)){
			trainingCharge.setCfbillstate(5L);
		}else{
			trainingCharge.setCfbillstate(4L);
		}
		return trainingChargeMapper.updateByPrimaryKeySelective(trainingCharge);
	}

	@Override
	public TrainingChargeEntry selTrainingChargeByCar(String car, String userSession) {
		Map map = new HashMap();
			map.put("car",car);
			map.put("companyId",userSession);
		TrainingChargeEntry trainingChargeEntry = new TrainingChargeEntry();
		List<TrainingChargeEntry> list = trainingChargeEntryMapper.selTrainingChargeByCar(map);
		if(!list.isEmpty())
			trainingChargeEntry = list.get(0);
		return trainingChargeEntry;
	}

	@Override
	public List<TrainingChargeEntry> selTrainingChargeByCar(String fcompanyid, String cfcar, String drivingName, String beginTime, String endTime) {
		Map map = new HashMap();
			map.put("companyId",fcompanyid);
			map.put("cfcar",cfcar);
			map.put("drivingName",drivingName);
			map.put("beginTime",beginTime);
			map.put("endTime",endTime);
		return trainingChargeEntryMapper.selTrainingChargeByCarInfo(map);
	}
}
